package com.frank.loadinglibrary.special.cool.ball;

import com.frank.loadinglibrary.special.cool.CoolLoadingComponent;
import com.frank.loadinglibrary.utils.SimpleAnimatorListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;

public class FirstCoolBall extends CoolBall {
    //第一个出现初始球的大小
    private final int FIRST_CIRCLE_START_RADIUS = 40;
    //环绕行星最大半径
    private final int SECOND_CIRCLE_MAX_RADIUS = 10;

    //黄色圆的线粗最小值
    private final int THIRD_CIRCLE_MIN_STROKEWIDTH = 15;

    // 正在结束,只绘制结束动画
    private boolean isMoveEnd = false;

    private Paint mPaint;
    private RectFloat mRectF;
    //蓝球半径
    private float mFirCirRadius;
    //环绕行星半径
    private int mSecCirRadius;
    //黄环圆半径
    private int mThiCirRadius;

    //环绕行星角度间隔
    private final int DIVIDE_DEGREES = 36;
    //环绕圆旋转角度
    private int mAroundCirDegrees;
    //环绕弧线旋转角度
    private int mAroundArcDegrees;
    //环绕弧线长度
    private int mAroundArcLength;

    //黄环线粗
    private int mThiCirStrokeWidth;

    //最后的蓝圆，对应最开始的蓝圆
    private int mPreFirCirRadius;


    private AnimatorValue mFirAnimator;
    private AnimatorValue mSecAnimator;
    private AnimatorGroup mThrAnimator;
    private AnimatorValue mFouAnimator;
    private AnimatorValue mFifAnimator;


    private int mEndCirIRadius;
    private int mEndCirMRadius;
    private int mEndCirORadius;
    private AnimatorValue mEndAnimator;

    public FirstCoolBall(float x, float y) {
        super(x, y);
        initAnim();
    }

    @Override
    protected void initConfig() {
        super.initConfig();
        mEndMovingLength = -25;
        init();

    }


    private void init() {
        mRectF = new RectFloat(getCurX() - 45, getCurY() - 45, getCurX() + 45, getCurY() + 45);
        initPaint();
    }

    @Override
    public void reset() {
        super.reset();
        isMoveEnd = false;
    }

    @Override
    public void drawSelf(Canvas canvas) {
        super.drawSelf(canvas);
        if (!isMoveEnd) {
            //蓝色球
            mPaint.setColor(Color.BLUE);
            mPaint.setStrokeWidth(mFirCirRadius);
            canvas.drawCircle(getCurX(), getCurY(), mFirCirRadius / 2f, mPaint);

            //绘制篮球环绕行星
            mPaint.setColor(CoolLoadingComponent.GREEN);
            for (int i = 0; i < 10; i++) {
                canvas.save();
                mPaint.setStrokeWidth(mSecCirRadius);
                canvas.rotate(90 + i * DIVIDE_DEGREES + mAroundCirDegrees, getCurX(), getCurY());
                canvas.drawCircle(getCurX(), getCurY() - 60, mSecCirRadius / 2, mPaint);
                canvas.restore();
            }

            //环绕结束,黄色球包围
            mPaint.setColor(CoolLoadingComponent.YELLOW);
            mPaint.setStrokeWidth(mThiCirStrokeWidth);
            canvas.drawCircle(getCurX(), getCurY(), mThiCirRadius / 2, mPaint);

            //扇形弧线
            mPaint.setColor(Color.BLUE);
            mPaint.setStrokeWidth(20);
            canvas.drawArc(mRectF, new Arc(mAroundArcDegrees, mAroundArcLength, false), mPaint);

            //绘制最后的篮球
            mPaint.setColor(Color.BLUE);
            mPaint.setStrokeWidth(mPreFirCirRadius);
            canvas.drawCircle(getCurX(), getCurY(), mPreFirCirRadius / 2, mPaint);

        }

        if (mIsEnd) {
            if (!mIsEndAnimStart) {
                mEndAnimator.start();
                mIsEndAnimStart = true;
            }
            mPaint.setStyle(Paint.Style.FILL_STYLE);
            mPaint.setColor(CoolLoadingComponent.BLUE);
            canvas.drawCircle(getCurX(), getCurY(), mEndCirIRadius, mPaint);
            mPaint.setColor(CoolLoadingComponent.RED);
            canvas.drawCircle(getCurX(), getCurY(), mEndCirMRadius, mPaint);
            mPaint.setColor(CoolLoadingComponent.YELLOW);
            canvas.drawCircle(getCurX(), getCurY(), mEndCirORadius, mPaint);
            mPaint.setStyle(Paint.Style.STROKE_STYLE);
            return;
        }
    }

    @Override
    protected void initAnim() {
        initFirstAnimatitor();
        initSecAnimatitor();
        initThrAnimatitor();
        initFouAnimatitor();
        initmFifAnimatitor();
    }


    private void initFirstAnimatitor() {
        mFirAnimator = new AnimatorValue();
        mFirAnimator.setLoopedCount(0);
        mFirAnimator.setDuration(500);
        mFirAnimator.setValueUpdateListener((animatorValue, v) -> {
                    float value = 0;
                    if (v < 0.5f) {
                        value = FIRST_CIRCLE_START_RADIUS + 10 * v;
                    } else {
                        value = FIRST_CIRCLE_START_RADIUS + 10 - 10 * v;
                    }
                    mFirCirRadius = value;
                }
        );
        mFirAnimator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                mPreFirCirRadius = 0;//将顶层的蓝色圆半径置0;
                mThiCirRadius = 0;
                mThiCirStrokeWidth = 0;
                mSecAnimator.start();
            }
        });
    }

    private void initSecAnimatitor() {
        //环绕行星动画
        mSecAnimator = new AnimatorValue();
        mSecAnimator.setDuration(2000);
        mSecAnimator.setLoopedCount(0);
        mSecAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                float zoroToOne = v;
                //环绕行星的旋转角度
                mAroundCirDegrees = (int) (90 + 180 * zoroToOne);
                if (zoroToOne < 0.5f) {
                    zoroToOne = zoroToOne * 2;
                    //环绕行星的大小变化
                    mSecCirRadius = (int) (zoroToOne * SECOND_CIRCLE_MAX_RADIUS);
                } else {
                    zoroToOne = (1 - zoroToOne) * 2;
                    mSecCirRadius = (int) (zoroToOne * SECOND_CIRCLE_MAX_RADIUS);
                }
            }
        });
        mSecAnimator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                mFouAnimator.start();
                mThrAnimator.start();
            }
        });
    }

    private void initThrAnimatitor() {
        //环绕结束后蓝色圆有一个变大的动作

        AnimatorValue mThrAnimator_one = new AnimatorValue();
        mThrAnimator_one.setLoopedCount(0);
        mThrAnimator_one.setDuration(166);
        mThrAnimator_one.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                mFirCirRadius = FIRST_CIRCLE_START_RADIUS + 10 * v;
            }
        });

        AnimatorValue mThrAnimator_sec = new AnimatorValue();
        mThrAnimator_sec.setLoopedCount(0);
        mThrAnimator_sec.setDuration(167);
        mThrAnimator_sec.setValueUpdateListener((animatorValue, v) -> mFirCirRadius = FIRST_CIRCLE_START_RADIUS + 10 - 30 * v);

        AnimatorValue mThrAnimator_thr = new AnimatorValue();
        mThrAnimator_thr.setLoopedCount(0);
        mThrAnimator_thr.setDuration(167);
        mThrAnimator_thr.setValueUpdateListener((animatorValue, v) -> mFirCirRadius = 20);


        mThrAnimator = new AnimatorGroup();
        mThrAnimator.setLoopedCount(0);
        mThrAnimator.runSerially(mThrAnimator_one, mThrAnimator_sec, mThrAnimator_thr);
    }

    private void initFouAnimatitor() {
        //环绕结束黄色的圆出现覆盖并缩小
        mFouAnimator = new AnimatorValue();
        mFirAnimator.setDuration(1000);
        mFouAnimator.setLoopedCount(0);
        mFouAnimator.setValueUpdateListener((animatorValue, v) -> {
                    mThiCirRadius = (int) (20 + 80 * (1 - v));//变化范围60～20.
                    mThiCirStrokeWidth = 15 + (int) ((float) v * THIRD_CIRCLE_MIN_STROKEWIDTH);
                }
        );
        mFouAnimator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                mFifAnimator.start();
            }
        });
    }


    private void initmFifAnimatitor() {
        //弧形动画旋转启动,弧形角度长度变化＋蓝色球放大动画
        mFifAnimator = new AnimatorValue();
        mFifAnimator.setLoopedCount(0);
        mFifAnimator.setDuration(2000);
        mFifAnimator.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);
        mFifAnimator.setValueUpdateListener((animatorValue, v) -> {
            float zoroToOne = 2.0f * v;
            if (zoroToOne < 0.5f) {
                mAroundArcDegrees = (int) (-120 + 880 * zoroToOne);
                zoroToOne = zoroToOne * 2;
                mAroundArcLength = (int) (180 * zoroToOne);
            } else if (zoroToOne <= 1.000f) {
                mAroundArcDegrees = (int) (-120 + 880 * zoroToOne);
                zoroToOne = (1 - zoroToOne) * 2;
                mAroundArcLength = (int) (180 * zoroToOne);
            } else {
                mAroundArcLength = 0;
                zoroToOne = zoroToOne - 1;
                mPreFirCirRadius = (int) (40 * zoroToOne);
            }
        });
        mFifAnimator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                if (mAnimatorStateListen != null) {
                    mAnimatorStateListen.onAnimatorEnd();
                }
            }
        });
    }

    @Override
    public void initEndAnim() {
        mEndAnimator = new AnimatorValue();
        mEndAnimator.setDuration(mDuration);
//        mEndAnimator.setRepeatCount(2);
        mEndAnimator.setValueUpdateListener((ani, v) -> {
            float zoroToOne = (float) 2 * v;
            if (zoroToOne <= 1.0f) {
                mCurX = (int) (mPerX + zoroToOne * mEndMovingLength);
                mEndCirIRadius = (int) (MAX_RADIUS_CIRCLE * zoroToOne);
                if (zoroToOne <= 0.5f) {
                    zoroToOne = 2 * zoroToOne;
                } else {
                    zoroToOne = 1 - 2 * (zoroToOne - 0.5f);
                }
                mEndCirMRadius = (int) (MAX_RADIUS_CIRCLE * zoroToOne);
            } else {
                if (!isMoveEnd) {
                    isMoveEnd = true;
                    if (mAnimatorStateListen != null) {
                        System.out.println("onMoveEnd first");
                        mAnimatorStateListen.onMoveEnd();
                    }
                }
                zoroToOne = 2 - zoroToOne;
                mEndCirIRadius = (int) (MAX_RADIUS_CIRCLE * zoroToOne);
                if (zoroToOne >= 0.5f) {
                    zoroToOne = (1.0f - zoroToOne) * 2;
                } else {
                    zoroToOne = zoroToOne * 2;
                }
                mEndCirORadius = (int) (MAX_RADIUS_CIRCLE * zoroToOne);
            }
        });
        mEndAnimator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                if (mAnimatorStateListen != null) {
                    mAnimatorStateListen.onAllAnimatorEnd();
                }
            }
        });
    }

    @Override
    public void startAnim() {
        super.startAnim();
        mFirAnimator.start();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
    }
}
